package cn.net.yugu.doraemon.common.jackson.serializer;

import java.io.IOException;
import java.time.Instant;
import java.util.function.ToLongFunction;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

/**
 * @author xiangzr
 */
public class InstantLongJsonSerializer extends JsonSerializer<Instant> {

  private final ToLongFunction<Instant> getEpochMillis = Instant::toEpochMilli;

  @Override
  public void serialize(Instant value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeNumber(getEpochMillis.applyAsLong(value));
  }

}
